/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import virtPresenter.event.*;
	import virtPresenter.model.virtPresenterLocator;
	
	/**
	 * Command lets the application go to the lecture and timepoint given
	 * by a search result.
	 * */
	public class GoToLectureCommand implements ICommand
	{	
		private var model : virtPresenterLocator = virtPresenterLocator.getInstance();	
		
		/**
		 * Handles a GoToLectureEvent. If the current lecture is the lecture from the search result
		 * the video is set to the timepoint of the search result. Otherwise a ChangesLectureFolderEvent
		 * is dispatched.
		 * 
		 * @see virtPresenter.event.GoToLectureEvent
		 * 
		 * @see virtPresenter.event.ChangedLectureFolderEvent
		 * */				
		public function execute(event:CairngormEvent):void
		{				
			var e : GoToLectureEvent = GoToLectureEvent(event);
			var folder:String=e.lecturesource;
			var foundIndex:Boolean=false;
			model.initPoint=e.time/1000;
			var time_sec:Number=e.time/1000+0.5;			
			
			if(model.lectureFolder==folder)
			{
				model.netStream.seek(time_sec);
			}
			else
			{
				for (var i:int=0;i<model.lectures.length;i++)
				{
					if (model.lectures[i].valueOf().source==folder)
					{
						CairngormEventDispatcher.getInstance().dispatchEvent(new ChangedLectureFolderEvent(i));
						foundIndex=true;
					}					
				}
				
				if (foundIndex!=true){
					trace("GoToLectureCommand. Could not find index for lecture with folder: "+folder);
				}
				
			}
		}		
		
	}
}